﻿using STHPMS1.Common.C_Entity;
using STHPMS1.Common.C_Entity.Stmpms1;
using STHPMS1.ICore;
using STHPMS1.ICore.I_Core;
using STHPMS1.ICore.I_UI;
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web;
using STHPMS1.ICore.I_Serv;
using STHPMS1.ICore.I_DAL;


namespace STHPMS1.Web.W_Controller.Decision.Fondos
{
    [RoutePrefix("api/dfcostmodpara")]
    public class DF_CostmodParaController : STHPMS1.Web.W_Core.MyController
    {
        [Route("")]
        [Route("page/{page?}", Name = "GetdfcostmodparaByPage")]
        [Route("rows/{rows?}")]
        [Route("page/{page?}/rows/{rows?}")]
        public IHttpActionResult Get(string road = null, int page = 1, int rows = 10)
        {
            int p = page;
            int s = rows;

            PagingInfo info = new PagingInfo();
            info.PageIndex = p > 0 ? p - 1 : 0;
            info.PageSize = s;
            info.TotalRecords = 0;

            Expression<Func<Df_Costmod_Para, bool>> exp = null;
            //if (road != null && road.ToString().Length > 0)
            //    exp = pre => pre.Rdwayid == road.ToString();

            var data = this.Scene.Bll.Df_Costmod_Para.Get<Df_Costmod_Para>(exp, info).ToArray();
            if (data == null) return NotFound();

            var result = new { rows = data, total = info.TotalRecords };

            return Ok(result);
        }

        [Route("")]
        public bool Post()
        {
            IKeyGenrateService serv = MyApp.Locator.GetInstance<IKeyGenrateService>();
            string anniname = HttpContext.Current.Request.Form["Anni_Name"]; //资金模型名称
            string roadnetname = HttpContext.Current.Request.Form["Roadnet_Name"];//路网名称
            int Year_Planing = Convert.ToInt32(HttpContext.Current.Request.Form["Year_Planing"]); //分析几年

            int anastype = 20010;
            if (Year_Planing == 1)
            {
                anastype = 20010;
            }
            else
            {
                anastype = 20011;
            }
            //int anastype = Convert.ToInt32(HttpContext.Current.Request.Form["Anas_Type"]); //分析类型:单年度分配，多年度分配
            int annutype = Convert.ToInt32(HttpContext.Current.Request.Form["Annu_Type"]); //资金类型：资金不受限，资金受限
            

            var data = this.Scene.Bll.Df_Costmod_Para.Get<Df_Costmod_Para>(pre => pre.Anni_Name == anniname, null).ToList<Df_Costmod_Para>();
            if (data.Count >= 1)
            {
                HttpContext.Current.Response.Write("资金模型参数名称已存在！");
                HttpContext.Current.Response.End();
                return false;
            }


            if (annutype == 20020)
            {
                Df_Costmod_Para info = new Df_Costmod_Para();
                string Analy_Year1 = HttpContext.Current.Request.Form["Analy_Year1"];
                //string Annu_Fundlim1 = HttpContext.Current.Request.Form["Annu_Fundlim1"];
                if (!string.IsNullOrEmpty(Analy_Year1) )
                {
                    info.Id = serv.NewGuid();
                    info.Anni_Name = anniname;
                    info.Roadnet_Name = roadnetname;
                    info.Anas_Type = anastype;
                    info.Annu_Type = annutype;
                    info.Analy_Year = Analy_Year1;
                    //info.Annu_Fundlim = Convert.ToDouble(Annu_Fundlim1);
                    this.Scene.Bll.Df_Costmod_Para.Save(info);
                    //return true;
                }

                string Analy_Year2 = HttpContext.Current.Request.Form["Analy_Year2"];
                //string Annu_Fundlim2 = HttpContext.Current.Request.Form["Annu_Fundlim2"];
                if (!string.IsNullOrEmpty(Analy_Year2))
                {
                    info.Id = serv.NewGuid();
                    info.Anni_Name = anniname;
                    info.Roadnet_Name = roadnetname;
                    info.Anas_Type = anastype;
                    info.Annu_Type = annutype;
                    info.Analy_Year = Analy_Year2;
                    //info.Annu_Fundlim = Convert.ToDouble(Annu_Fundlim2);
                    this.Scene.Bll.Df_Costmod_Para.Save(info);
                    //return true;
                }
                string Analy_Year3 = HttpContext.Current.Request.Form["Analy_Year3"];
                //string Annu_Fundlim3 = HttpContext.Current.Request.Form["Annu_Fundlim3"];
                if (!string.IsNullOrEmpty(Analy_Year3))
                {
                    info.Id = serv.NewGuid();
                    info.Anni_Name = anniname;
                    info.Roadnet_Name = roadnetname;
                    info.Anas_Type = anastype;
                    info.Annu_Type = annutype;
                    info.Analy_Year = Analy_Year3;
                    //info.Annu_Fundlim = Convert.ToDouble(Annu_Fundlim3);
                    this.Scene.Bll.Df_Costmod_Para.Save(info);
                    //return true;
                }
                string Analy_Year4 = HttpContext.Current.Request.Form["Analy_Year4"];
                //string Annu_Fundlim4 = HttpContext.Current.Request.Form["Annu_Fundlim4"];
                if (!string.IsNullOrEmpty(Analy_Year4))
                {
                    info.Id = serv.NewGuid();
                    info.Anni_Name = anniname;
                    info.Roadnet_Name = roadnetname;
                    info.Anas_Type = anastype;
                    info.Annu_Type = annutype;
                    info.Analy_Year = Analy_Year4;
                    //info.Annu_Fundlim = Convert.ToDouble(Annu_Fundlim4);
                    this.Scene.Bll.Df_Costmod_Para.Save(info);
                    //return true;
                }
                string Analy_Year5 = HttpContext.Current.Request.Form["Analy_Year5"];
                //string Annu_Fundlim5 = HttpContext.Current.Request.Form["Annu_Fundlim5"];
                if (!string.IsNullOrEmpty(Analy_Year5))
                {
                    info.Id = serv.NewGuid();
                    info.Anni_Name = anniname;
                    info.Roadnet_Name = roadnetname;
                    info.Anas_Type = anastype;
                    info.Annu_Type = annutype;
                    info.Analy_Year = Analy_Year5;
                    //info.Annu_Fundlim = Convert.ToDouble(Annu_Fundlim5);
                    this.Scene.Bll.Df_Costmod_Para.Save(info);
                    //return true;
                }
            }
            else
            {
                Df_Costmod_Para info = new Df_Costmod_Para();
                string Analy_Year1 = HttpContext.Current.Request.Form["Analy_Year1"];
                string Annu_Fundlim1 = HttpContext.Current.Request.Form["Annu_Fundlim1"];
                if (!string.IsNullOrEmpty(Analy_Year1) && !string.IsNullOrEmpty(Annu_Fundlim1))
                {
                    info.Id = serv.NewGuid();
                    info.Anni_Name = anniname;
                    info.Roadnet_Name = roadnetname;
                    info.Anas_Type = anastype;
                    info.Annu_Type = annutype;
                    info.Analy_Year = Analy_Year1;
                    info.Annu_Fundlim = Convert.ToDouble(Annu_Fundlim1);
                    this.Scene.Bll.Df_Costmod_Para.Save(info);
                    //return true;
                }

                string Analy_Year2 = HttpContext.Current.Request.Form["Analy_Year2"];
                string Annu_Fundlim2 = HttpContext.Current.Request.Form["Annu_Fundlim2"];
                if (!string.IsNullOrEmpty(Analy_Year2) && !string.IsNullOrEmpty(Annu_Fundlim2))
                {
                    info.Id = serv.NewGuid();
                    info.Anni_Name = anniname;
                    info.Roadnet_Name = roadnetname;
                    info.Anas_Type = anastype;
                    info.Annu_Type = annutype;
                    info.Analy_Year = Analy_Year2;
                    info.Annu_Fundlim = Convert.ToDouble(Annu_Fundlim2);
                    this.Scene.Bll.Df_Costmod_Para.Save(info);
                    //return true;
                }
                string Analy_Year3 = HttpContext.Current.Request.Form["Analy_Year3"];
                string Annu_Fundlim3 = HttpContext.Current.Request.Form["Annu_Fundlim3"];
                if (!string.IsNullOrEmpty(Analy_Year3) && !string.IsNullOrEmpty(Annu_Fundlim3))
                {
                    info.Id = serv.NewGuid();
                    info.Anni_Name = anniname;
                    info.Roadnet_Name = roadnetname;
                    info.Anas_Type = anastype;
                    info.Annu_Type = annutype;
                    info.Analy_Year = Analy_Year3;
                    info.Annu_Fundlim = Convert.ToDouble(Annu_Fundlim3);
                    this.Scene.Bll.Df_Costmod_Para.Save(info);
                    //return true;
                }
                string Analy_Year4 = HttpContext.Current.Request.Form["Analy_Year4"];
                string Annu_Fundlim4 = HttpContext.Current.Request.Form["Annu_Fundlim4"];
                if (!string.IsNullOrEmpty(Analy_Year4) && !string.IsNullOrEmpty(Annu_Fundlim4))
                {
                    info.Id = serv.NewGuid();
                    info.Anni_Name = anniname;
                    info.Roadnet_Name = roadnetname;
                    info.Anas_Type = anastype;
                    info.Annu_Type = annutype;
                    info.Analy_Year = Analy_Year4;
                    info.Annu_Fundlim = Convert.ToDouble(Annu_Fundlim4);
                    this.Scene.Bll.Df_Costmod_Para.Save(info);
                    //return true;
                }
                string Analy_Year5 = HttpContext.Current.Request.Form["Analy_Year5"];
                string Annu_Fundlim5 = HttpContext.Current.Request.Form["Annu_Fundlim5"];
                if (!string.IsNullOrEmpty(Analy_Year5) && !string.IsNullOrEmpty(Annu_Fundlim5))
                {
                    info.Id = serv.NewGuid();
                    info.Anni_Name = anniname;
                    info.Roadnet_Name = roadnetname;
                    info.Anas_Type = anastype;
                    info.Annu_Type = annutype;
                    info.Analy_Year = Analy_Year5;
                    info.Annu_Fundlim = Convert.ToDouble(Annu_Fundlim5);
                    this.Scene.Bll.Df_Costmod_Para.Save(info);
                    //return true;
                }
            }

            return true;
            //ISthpms1Dao dao = this.Scene.Dal.Sthpms1;
            //dao.Delete<Df_Costmod_Para>(p => p.Roadnet_Name == roadnetname && p.Anas_Type == anastype);
            

            
            
        }


        [Route("")]
        public void Delete(Df_Costmod_Para entity)
        {
            //this.Scene.Bll.Df_Costmod_Para.Remove(entity);
            if (entity.Anas_Type == 20010)
            {
                ISthpms1Dao dao = this.Scene.Dal.Sthpms1;
                dao.Delete<Df_Costmod_Para>(p => p.Id == entity.Id);
            }
            else
            {
                ISthpms1Dao dao = this.Scene.Dal.Sthpms1;
                dao.Delete<Df_Costmod_Para>(p => p.Roadnet_Name == entity.Roadnet_Name && p.Anas_Type == entity.Anas_Type);
            }
            
        }

        /// <summary>
        /// 获取资金模型名称
        /// </summary>
        /// <param name="annutype"></param>
        /// <returns></returns>
        [Route("anniname")]
        public dynamic GetAnniName(string annutype=null)
        {
            var innerJoinQuery =
                from road in this.Scene.Bll.Df_Costmod_Para.GetAnniName(annutype)
                select new { id = road, text = road };
            return innerJoinQuery;
        }

        /// <summary>
        /// 获取资金模型名称下所有分析年度
        /// </summary>
        /// <param name="annutype"></param>
        /// <returns></returns>
        [Route("AnalyYear")]
        public dynamic GetAnalyYear(string anni_name)
        {
            var innerJoinQuery =
                from road in this.Scene.Bll.Df_Costmod_Para.GetAnaly_Year(anni_name)
                select new { id = road, text = road };
            return innerJoinQuery;
        }

    }
}